﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using MyBet.Helpers;
using MyBet.Class;

namespace MyBet
{
	public partial class EventDataGrid : UserControl
	{

        protected FrameworkElement _visibleItem;
        private int _selectedEventId;
        private int _visibleOffset;
        private bool _isVisibilityNeeded;
        private EventServiceReference.EventObject defaultEvent;

        /// <summary>
        /// Liste des événements
        /// </summary>
        public DataGrid EventsList
        {
            get { return eventsDataGrid; }
        }

        /// <summary>
        /// Identifiant de l'événement à sélectionner
        /// </summary>
        protected int SelectedEventId
        {
            get { return _selectedEventId; }
            set { _selectedEventId = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        protected int VisibleOffset
        {
            get { return _visibleOffset; }
            set { _visibleOffset = value; }
        }

        /// <summary>
        /// Indique que l'item à rendre visible n'a pas été sélectionné
        /// </summary>
        protected bool IsVisibilityNeeded
        {
            get { return _isVisibilityNeeded; }
            set { _isVisibilityNeeded = value; }
        }

        /// <summary>
        /// Evénement sélectionné par défaut (pari en cours)
        /// </summary>
        protected EventServiceReference.EventObject DefaultEvent
        {
            get { return defaultEvent; }
            set { defaultEvent = value; }
        }

        /// <summary>
        /// Constructeur
        /// </summary>
		public EventDataGrid()
		{
			// Required to initialize variables
			InitializeComponent();
		}

        public void LoadEvents(int currentEventId, ObservableCollection<EventServiceReference.EventObject> eventList)
        {
            SelectedEventId = currentEventId;
            VisibleOffset = 0;
            eventsDataGrid.ClearValue(DataGrid.ItemsSourceProperty);
            eventsDataGrid.ItemsSource = eventList;
            eventsDataGrid.DataContext = eventList;
        }

        /// <summary>
        /// Affichage d'une ligne avec un événement
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void eventsDataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            var currentEvent = (EventServiceReference.EventObject) e.Row.DataContext;
            DataGridColumn column = this.eventsDataGrid.Columns[0];
            FrameworkElement feColumn = column.GetCellContent(e.Row);
            StackPanel stack = feColumn as StackPanel;

            if (stack != null && currentEvent != null)
            {
                String currentStyle = String.Empty;
                VisibleOffset++;

                // Choix du style en fonction du statut de l'événement
                if (currentEvent._eventId == SelectedEventId)
                {
                    currentStyle = "EventsList{0}_Current";
                }
                else if (currentEvent._closeDate < DateTime.UtcNow)
                {
                    currentStyle = "EventsList{0}_Closed";
                }
                else
                {
                    currentStyle = "EventsList{0}_Normal";
                }

                TextBlock eventTextBlock = new TextBlock();
                eventTextBlock.Text = String.Format("{0} {1}", LangHelper.GetString("DayLabel").Trim(), currentEvent._dayNumber);
                eventTextBlock.Style = Application.Current.Resources[String.Format(currentStyle,"Title")] as Style;
                stack.Children.Add(eventTextBlock);
                TextBlock dateTextBlock = new TextBlock();
                dateTextBlock.Text = String.Format("{0}", UserTimezone.FromUTC(currentEvent._closeDate));
                dateTextBlock.Style = Application.Current.Resources[String.Format(currentStyle, "Date")] as Style;
                stack.Children.Add(dateTextBlock);

                if (currentEvent._eventId == SelectedEventId)
                {
                    DefaultEvent = currentEvent;
                    IsVisibilityNeeded = true;
                    if (VisibleOffset > 7)
                    {
                        VisibleOffset = 0;
                    }
                    else
                    {
                        VisibleOffset = 7;
                    }
                }

                // Astuce pour que l'élément sélectionné ne soit pas le dernier
                // mais se retrouve en milieu de liste
                if ((IsVisibilityNeeded) && (VisibleOffset == 7))
                {
                    _visibleItem = stack;
                    IsVisibilityNeeded = false;
                }

            }

        }

        /// <summary>
        /// Sélection de l'événement en cours
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void eventsDataGrid_Loaded(object sender, RoutedEventArgs e)
        {
            eventsDataGrid.SelectedItem = DefaultEvent;
            if (_visibleItem != null)
            {
                scroll.ScrollIntoView(_visibleItem);
            }
            else
            {
                scroll.ScrollToBottom();
            }
        }

        private void eventsDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }


	}
}